package demo1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class Test1 {
    public static ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        // write code here
        Collections.sort(nums1);
        Collections.sort(nums2);

        // 使用HashSet去重
        HashSet<Integer> h1 = new HashSet<>(nums1);
        // 将HashSet再转换回ArrayList
        ArrayList<Integer> nums1ArrayList = new ArrayList<>(h1);

        // 使用HashSet去重
        HashSet<Integer> h2 = new HashSet<>(nums2);
        // 将HashSet再转换回ArrayList
        ArrayList<Integer> nums2ArrayList = new ArrayList<>(h2);

        int i = 0;
        int j = 0;
        ArrayList<Integer> list = new ArrayList<>();
        while (true){
            int a = nums1ArrayList.get(i);
            int b = nums2ArrayList.get(j);
            if (a == b) {
                list.add(a);
                i++;
                j++;
            }else if (a < b){
                i++;
            }else {
                j++;
            }
            if (i == nums1ArrayList.size() || j == nums2ArrayList.size()) {
                break;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        ArrayList<Integer> nums1 = new ArrayList<>();
        nums1.add(2);
        nums1.add(1);
        nums1.add(2);
        ArrayList<Integer> nums2 = new ArrayList<>();
        nums2.add(2);
        nums2.add(3);
        ArrayList<Integer> num = intersection(nums1, nums2);
        System.out.println(num);
    }

    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String s = in.next();
        int i = 0;
        String ret = s;

        while(!s.isEmpty()) {
            char a = s.charAt(i);
            char b = s.charAt(i+1);
            if(a == b) {
                s = ret.substring(0, i);
                s += ret.substring(i+2, ret.length());
                ret = s;

                i--;
                if (i == -1) {
                    i = 0;
                }
            }else{
                i++;
            }
            if(i+1 == s.length()) {
                break;
            }
        }

        if(s.length()==0) {
            System.out.println("0");
        }else{
            System.out.println(s);
        }
    }
}
